package org.mparts.client.utils;

import java.util.List;

import org.mparts.client.MpartsService;
import org.mparts.client.MpartsServiceAsync;
import org.mparts.client.content.Strings;
import org.mparts.shared.MpartsRequestFactory;
import org.mparts.shared.proxy.AutoProxy;
import org.mparts.shared.proxy.EngineProxy;
import org.mparts.shared.proxy.PartgroupProxy;

import com.google.gwt.core.client.GWT;
import com.google.inject.Inject;
import com.google.web.bindery.requestfactory.shared.Receiver;

public class Utils {
	private static Strings strings = null;
	
	private static List<AutoProxy> autoList = null;
	private static List<PartgroupProxy> partgroupList = null;
	private static List<EngineProxy> engineList = null;
	
	@Inject
	private static MpartsRequestFactory rf;
	
	private final static MpartsServiceAsync mpartsService = GWT
			.create(MpartsService.class);
	
	private static List<String> userData;
	
	private static Long currentStoreId = 1L;


	public static Strings getStrings() {
		if ( strings == null ) {
			strings = (Strings) GWT.create(Strings.class);
		}
		return strings;
	}
	
	
	public static MpartsServiceAsync getMpartsService() {
		return mpartsService;
	}
	
	public static void init() {
		rf.mpartsRequest().findAllPartgroups().fire(new Receiver<List<PartgroupProxy>>() {

			@Override
			public void onSuccess(List<PartgroupProxy> response) {
				partgroupList = response;
			}
		});
		
		rf.mpartsRequest().findAutos().fire(new Receiver<List<AutoProxy>>() {

			@Override
			public void onSuccess(List<AutoProxy> response) {
				autoList = response;
			}
		});
		
		rf.mpartsRequest().findAllEngines().fire(new Receiver<List<EngineProxy>>() {

			@Override
			public void onSuccess(List<EngineProxy> response) {
				engineList = response;
			}
		});
	}
	


	public static List<AutoProxy> getAutoList() {
		return autoList;
	}


	public static List<PartgroupProxy> getPartgroupList() {
		return partgroupList;
	}


	public static void setAutoList(List<AutoProxy> autoList) {
		Utils.autoList = autoList;
	}


	public static void setPartgroupList(List<PartgroupProxy> partgroupList) {
		Utils.partgroupList = partgroupList;
	}


	public static List<String> getUserData() {
		return userData;
	}


	public static List<EngineProxy> getEngineList() {
		return engineList;
	}


	public static void setEngineList(List<EngineProxy> engineList) {
		Utils.engineList = engineList;
	}


	public static void setUserData(List<String> userData) {
		Utils.userData = userData;
	}
	
	public static boolean isManager() {
		boolean result = false;
		
		if ( userData != null && userData.size() >= 4 ) {
			result = "1".equals(userData.get(3));
		}
		
		return result;
	}
	
	
	public static native void recordAnalyticsHit(String pageName) /*-{
    	$wnd._gaq.push(['_trackPageview', pageName]);
	}-*/;


	public static Long getCurrentStoreId() {
		return currentStoreId;
	}


	public static void setCurrentStoreId(Long currentStoreId) {
		Utils.currentStoreId = currentStoreId;
	}	
	
	public static String getCityName() {
		return currentStoreId == 2L ? "Омск" : "Новосибирск";
	}

	
}
